package com.atmc.controller;

import com.atmc.bean.Code;
import com.atmc.service.CodeService;
import com.atmc.service.OpenFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class CodeFeignController {
    @Autowired
    OpenFeignService openFeignService;
    @Autowired
    CodeService codeService;

    //首页跳转到账号管理信息
    @RequestMapping("/accountlist")
    public String accountlist(Model model){
        List<Code> list = openFeignService.codelist();
        model.addAttribute("msg",list);
        return "code/account";
    }


    //删除一条账号记录
    @RequestMapping("/tocodedelete/{acco_id}")
    public String codedeleteOne(@PathVariable("acco_id")Integer acco_id, Model model){
        codeService.deleteOne(acco_id);

        List<Code> list = openFeignService.codelist();
        model.addAttribute("msg",list);

        return "code/account";
    }
    //跳转到修改密码页面
    @RequestMapping("/tocodeupdate/{acco_name}")
    public String toUpdateCode(@PathVariable("acco_name")String acco_name,Model model){
        model.addAttribute("acco_name",acco_name);
        return "code/accountUpdate";
    }
    //修改一条账号记录
    @RequestMapping("/tocodeupdatesuccess")
    public String tocodeupdatesuccess(@RequestParam("acco_name")String acco_name,
                                      @RequestParam("acco_password")String acco_password
            ,Model model){
        Code code=new Code(null,acco_name,acco_password,null,null);
        codeService.updateOne(code);
        List<Code> list = openFeignService.codelist();
        model.addAttribute("msg",list);
        return "code/account";
    }
    //添加一条账号记录
    @RequestMapping("/toinsertcode")
    public String toinsertcode(@RequestParam("acco_name")String acco_name,
                               @RequestParam("acco_password")String acco_password,
                               Model model
                               ){
        Code code=new Code(null,acco_name,acco_password,null,null);

        codeService.insertOne(code);
        List<Code> list = openFeignService.codelist();
        model.addAttribute("msg",list);
        return "bill/insertsuccess";
    }
}
